package com.szj.spushrtmp.activity;

import android.Manifest;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.szj.spushrtmp.R;
import com.szj.spushrtmp.push.SPush;
import com.szj.spushrtmp.util.ResultCallBack;
import com.szj.spushrtmp.util.SPermissionX;

import java.util.ArrayList;

/**
 * @ClassName: LiveStreamingActivity
 * @Author: s10g
 * @CreateDate: 7/28/23$ 7:10 PM$
 * TODO
 */
public class LiveStreamingActivity extends AppCompatActivity {

    private SPush push;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_streaming);

        ArrayList<String> list = new ArrayList<>();
        list.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        list.add(Manifest.permission.CAMERA);
        list.add(Manifest.permission.RECORD_AUDIO);

        SPermissionX.request(this, list, new ResultCallBack<Boolean>() {
            @Override
            public void success(Boolean aBoolean) {
                if (aBoolean) {
                    initView();
                } else {
                    showToast("权限未全部开启");
                }

            }

            @Override
            public void fail(Throwable throwable) {

            }
        });

    }

    private void initView() {
        SurfaceView surfaceView = findViewById(R.id.surface_view);
        Button btn_switch = findViewById(R.id.btn_switch);
        Button btn_start = findViewById(R.id.btn_start);
        Button btn_stop = findViewById(R.id.btn_stop);

        push = new SPush(this, surfaceView);

        btn_switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                push.switchCamera();
            }
        });

        btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                push.startPlay("rtmp://192.144.213.57/s10g");
//                push.startPlay("rtmp://al.direct.huya.com/huyalive/1279515754272-1279515754272-7261912082905427420-2559031632000-10057-A-1690796304-1?seq=1691046041398&type=simple");
//                push.startPlay("rtmp://hs.direct.huya.com/huyalive/");
            }
        });

        btn_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                push.stopPlay();
            }
        });
    }


    void showToast(Object value) {
        Toast.makeText(this, value.toString(), Toast.LENGTH_SHORT).show();
    }
}
